package com.nidu.demo.user.model;

import com.alibaba.cola.exception.BizException;
import com.nidu.demo.base.BaseDeletableEntity;
import com.nidu.demo.base.BaseEntity;
import com.nidu.demo.base.BaseStatusEntity;
import com.nidu.demo.base.BaseTenantEntity;
import com.nidu.demo.common.enums.BooleanEnum;
import com.nidu.demo.common.enums.StatusEnum;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;
import java.util.Set;

/**
 * 用户领域对象
 *
 * @author auto
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class User extends BaseTenantEntity implements BaseStatusEntity, BaseDeletableEntity {

    private Long id;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 昵称
     */
    private String nickname;

    /**
     * 手机号
     */
    private String mobile;

    /**
     * 邮箱
     */
    private String email;

    /**
     * 头像
     */
    private String avatar;

    /**
     * 性别（0男 1女）
     */
    private Integer gender;

    /**
     * 公司ID
     */
    private Long companyId;

    /**
     * 登录IP
     */
    private String loginIp;

    /**
     * 登录时间
     */
    private LocalDateTime loginTime;

    /**
     * 排序值
     */
    private Integer sortOrder;

    /**
     * 状态
     */
    private Integer status;

    /**
     * 是否删除
     */
    private Boolean deleted;

    /**
     * 角色ID列表
     */
    private Set<Long> roleIds;

    /**
     * 部门ID列表
     */
    private Set<Long> deptIds;

    /**
     * 职位ID列表
     */
    private Set<Long> postIds;

    /**
     * 创建用户
     * @param username
     * @param password
     * @param name
     * @param mobile
     * @param email
     * @param avatar
     * @param gender
     * @param companyId
     * @param sortOrder
     * @return
     */
    public static User create(String username, String password, String name, String mobile,
            String email, String avatar, Integer gender, Long companyId, Integer sortOrder) {
        User user = new User();
        user.setUsername(username)
                .setPassword(password)
                .setNickname(name)
                .setMobile(mobile)
                .setEmail(email)
                .setAvatar(avatar)
                .setGender(gender)
                .setCompanyId(companyId)
                .setSortOrder(sortOrder);

        // 发布用户创建事件
//        DomainEventPublisher.publish(new UserCreatedEvent(
//                user.getId(),
//                user.getUsername(),
//                user.getEmail(),
//                user.getTenantId(),
//                LocalDateTime.now()
//        ));

        return user;
    }

    /**
     * 创建用户
     * @param username
     * @param password
     * @param nickname
     * @param mobile
     * @param email
     * @return
     */
    public static User create(String username, String password, String nickname, String mobile,  String email) {
        User user = new User();
        user.setNickname(nickname)
                .setUsername(username)
                .setPassword(password)
                .setMobile(mobile)
                .setNickname(nickname)
                .setSortOrder(0);
        return user;
    }

    /**
     * 更新用户
     * @param username
     * @param password
     * @param name
     * @param mobile
     * @param email
     * @param avatar
     * @param gender
     * @param companyId
     * @param sortOrder
     * @return
     */
    public static User update(Long id, String username, String password, String name, String mobile,
                              String email, String avatar, Integer gender, Long companyId, Integer sortOrder) {
        User user = new User();
        user.setId(id)
                .setUsername(username)
                .setPassword(password)
                .setNickname(name)
                .setMobile(mobile)
                .setEmail(email)
                .setAvatar(avatar)
                .setGender(gender)
                .setCompanyId(companyId)
                .setSortOrder(sortOrder);
        return user;
    }

    @Override
    public void isDeleted() {
        if (BooleanEnum.TRUE.getValue().equals(this.getDeleted())) {
            throw new BizException(ErrorCodeConstants.USER_NOT_FOUND.getCode(), ErrorCodeConstants.USER_NOT_FOUND.getMessage());
        }
    }

    @Override
    public void isDisable() {
        if (StatusEnum.DISABLE.getValue().equals(this.status)) {
            throw new BizException(ErrorCodeConstants.USER_STATUS_DISABLE.getCode(), ErrorCodeConstants.USER_STATUS_DISABLE.getMessage());
        }
    }

    public void setProfile(Set<Long> roleIds, Set<Long> deptIds, Set<Long> postIds) {
        this.roleIds = roleIds;
        this.deptIds = deptIds;
        this.postIds = postIds;
    }

}